perm filename PUPRT.C[11,HE] blob sn#688206 filedate 1982-12-06 generic text, type T, neo UTF8
/* LINTLIBRARY */
/*
 * puproute.c
 *
 * Main Pup Routing Table routines
 *
 * puproute()
 *
 * Jeffrey Mogul @ Stanford	20 July 1981
 *
 * The algorithms in this module are based upon a Xerox memo
 * entitled "Gateway Information Protocol (revised)", by Ed
 * Taft, May 30, 1979, filed on <Pup>GatewayInformation.press.
 *
 * Fixed bug about stale routing tables, 
 *	Bill Nowicki		November 10, 1981
 *
 */

#include <puplib.h>
#include <pupconstants.h>
#include <pupstatus.h>
#include <puproute.h>

struct	RouteEntry	RoutingTable[MAXNUMNETS];
int	ConnectedNets	= 0;		/* number of connected nets */
Net	OurNetNumber = 0;	/* what we think is our Net */

/*
 * puproute() -- called to find routing to remote host.
 */
puproute(Dst,ViaHost,ViaNet)
struct Port *Dst;		/* address of remote host */
Host *ViaHost;			/* returned first-hop address */
Net *ViaNet;			/* returned first-hop network */
{
	int	i;
	int	tryCount;

	if (ConnectedNets == 0)	/* uninitialized - we can do that much */
		InitRoutingTable(RoutingTable,&ConnectedNets);
	
	/* bug - - this test should be based upon hopcounts,
	 * since there could be many "ournetnumber"s
	 */
	if (Dst->net == OurNetNumber) {	/* easy route */
		*ViaHost = Dst->host;
		if (ViaNet) *ViaNet = Dst->net;
		return(OK);
	}

	/* search routing table for remote net */
	for ( tryCount = 0; tryCount < 3; tryCount++)
 	{
	 for (i = 0; i < ConnectedNets; i++) {
		if (RoutingTable[i].TargetNet == Dst->net) {
			/* found a route */
			if (RoutingTable[i].HopCount > MAXHOPS)
				continue;
			*ViaHost = RoutingTable[i].GatewayHost;
			if (ViaNet) *ViaNet = RoutingTable[i].GatewayNet;
			return(OK);
		}
	 }
	 InitRoutingTable(RoutingTable,&ConnectedNets);
	}
	/* could not find network */
	return(NOROUTE);
}